package com.SpringBoot.order.controller;

import com.SpringBoot.Util.Result;
import com.SpringBoot.order.mapper.OrderMapper;
import com.SpringBoot.order.pojo.Order;
import com.SpringBoot.order.service.OrderService;
import com.SpringBoot.orderDetail.pojo.OrderDetail;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单接口
 * </p>
 *
 * @author bxk
 * @since 2025-09-15
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;
    private final OrderMapper orderMapper;

    public OrderController(OrderService orderService, OrderMapper orderMapper) {
        this.orderService = orderService;
        this.orderMapper = orderMapper;
    }

    /**
     * 添加订单 + 订单详情
     */
    @PostMapping("/add")
    public Result addOrder(@RequestBody OrderRequest request) {
        return orderService.addOrderWithDetails(request.getOrder(), request.getDetails());
    }

    /**
     * 删除订单（逻辑删除）+ 删除订单详情
     */
    @PostMapping("/delete/{orderId}")
    public Result deleteOrder(@PathVariable Integer orderId) {
        return orderService.deleteOrder(orderId);
    }

    /**
     * 查询用户是否存在未完成订单
     */
    @GetMapping("/exist/{userId}")
    public Result existOrder(@PathVariable Integer userId) {
        Order order = orderService.selectExistOrder(userId);
        if (order != null) {
            return Result.success("存在未完成订单");
        } else {
            return Result.success("暂无未完成订单");
        }
    }

    /**
     * 封装请求体：订单 + 订单详情列表
     */
    public static class OrderRequest {
        private Order order;
        private List<OrderDetail> details;

        public Order getOrder() {
            return order;
        }

        public void setOrder(Order order) {
            this.order = order;
        }

        public List<OrderDetail> getDetails() {
            return details;
        }

        public void setDetails(List<OrderDetail> details) {
            this.details = details;
        }
    }

    @PostMapping("/updateByOrderId")
    public Result updateByOrderId(@RequestParam Integer orderId,@RequestParam Integer status){
        return orderService.updateByOrderId(orderId,status);
    }

    @GetMapping("/allAmount")
    public Result allAmount(){return orderService.allAmount();}

    @GetMapping("/selectUserOrder")
    public Result selectUserOrder(@RequestParam Integer userId){
        return orderService.selectUserOrder(userId);
    }

    @GetMapping("/selectByOrderId")
    public Result selectByOrderId(@RequestParam Integer orderId){
        return orderService.selectOrderByOrderId(orderId);
    }

    @GetMapping("/selectOrderByPage")
    public Result selectOrderByPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        return orderService.selectOrderByPage(pageNum,pageSize);
    }
}
